<?php

class BlackList extends Controller {
	var $msg = '';
	
	function BlackList()
	{
		parent::Controller();
	}

	function index()
	{
	 	if ($this->session->userdata('logged_in') != TRUE)
	    {
	        redirect('login/index');
	    }
	    
		$this->load->view('header');
		$this->load->view('black-list-detail-form');
		if($this->msg != null && count($this->msg) > 0)
			$msg = array('msg' => $this->msg);
		else
			$msg = null;
		$this->load->view('footer', $msg);
	}

	
	function stu_list() {
		$this->load->model('StudentModel');
		$data['studentTable'] = $this->StudentModel->getBlackList(null);
		$this->load->view('header');
		$this->load->view('black-stu-list-form', $data);
		$this->load->view('footer');
	}
	
	function listall() {
		$this->load->model('BlackListModel');
		$data['blacklistTable'] = $this->BlackListModel->getAll();
		$this->load->view('black-list-all-form',$data);
	}
	
	function detail($id) 
	{
		$this->load->model('BlackListModel');
		$data = $this->BlackListModel->getByID($id);		
		$this->load->view('header');
		$this->load->view('black-list-detail-form', $data);
		$msg = array('msg' => 'บันทึกข้อมูลเรียบร้อย');
		$this->load->view('footer', $msg);	
	}
	
	function details() 
	{
		$id = $this->input->post('txtId');
		$this->load->model('BlackListModel');
		$data = $this->BlackListModel->getByID($id);		
		$this->load->view('header');
		$this->load->view('black-list-detail-form', $data);
		$this->load->view('footer');	
	}
	
	function search() 
	{
		$data = array();
	    $data['name'] = $this->input->post('txtName') ? $this->input->post('txtName') : null;
	    $data['tel'] = $this->input->post('txtTel') ? $this->input->post('txtTel') : null;
	    $data['email'] = $this->input->post('txtEmail') ? $this->input->post('txtEmail') : null;
	    
			$this->load->model('BlackListModel');
			$data['blacklistTable'] = $this->BlackListModel->getByKeyword($data);
	    
		$this->load->view('header');
		$this->load->view('black-list-search-form', $data);
		$this->load->view('footer');	
	}
	
	
	
	function save()
	{
	 	if ($this->session->userdata('logged_in') != TRUE)
	    {
	        redirect('login/index');
	    }
	    $this->load->model('BlackListModel');
	    $data = array();
	    $data['first_name'] = $this->input->post('txtFname') ? $this->input->post('txtFname') : null;
	    $data['last_name'] = $this->input->post('txtLname') ? $this->input->post('txtLname') : null;
	    $data['status'] = $this->input->post('selStatus') ? $this->input->post('selStatus') : null;
	    $data['telephone'] = $this->input->post('txtTel') ? $this->input->post('txtTel') : null;
	    $data['detail'] = $this->input->post('txtDetail') ? $this->input->post('txtDetail') : null;
	    
	    $id = $this->input->post('txtID');
	    if(isset($id) && $id != null) {
	    	//update
	    	$data['backlist_id'] = $id;
	    	
		    if(isset($_FILES) && @$_FILES['txtImg']['error'] == '0') {
				$config['upload_path'] = './data-image/blacklist/';
				$config['allowed_types'] = 'gif|jpg|png';
				$config['max_size']	= '2048';
				$config['overwrite'] = TRUE;
				$config['file_name'] = $id;
				$this->load->library('upload', $config);
				if ( ! $this->upload->do_upload('txtImg'))
				{
					$this->msg = "ไม่สามารถ upload รูปภาพได้";			
				}
				else
				{
					$up = array('upload_data' => $this->upload->data());
					$data['image'] = $up['upload_data']['file_name'];
				}
			}
	    	
	    	$this->BlackListModel->update($data);
	    } else {
	    	// add
	    	$id = $this->BlackListModel->add($data);
	    	$data['backlist_id'] = $id;
	    	if(isset($_FILES) && @$_FILES['txtImg']['error'] == '0') {
				$config['upload_path'] = './data-image/blacklist/';
				$config['allowed_types'] = 'gif|jpg|png';
				$config['max_size']	= '2048';
				$config['overwrite'] = TRUE;
				$config['file_name'] = $id;
				$this->load->library('upload', $config);
				if ( ! $this->upload->do_upload('txtImg'))
				{
					$this->msg = "ไม่สามารถ upload รูปภาพได้";			
				}
				else
				{
					$up = array('upload_data' => $this->upload->data());
					$data['image'] = $up['upload_data']['file_name'];
				}
			}
	    	$this->BlackListModel->update($data);
	    }
	    $this->detail($id);
	}
	
	function delete(){
		if ($this->session->userdata('logged_in') != TRUE)
	    {
	        redirect('login/index');
	    }
	    
	    $this->load->model('BlackListModel');
	    $id = $this->input->post('txtDel');
	    for($i = 0; $i < count($id); $i++) {
	    	$this->BlackListModel->removeFromPage($id[$i]);
	    }	    
	    $this->search();
	}
}
?>
